package com.cskaoyan.service;

import com.cskaoyan.bean.bo.WxCommentPostBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.po.MarketUserExample;
import com.cskaoyan.bean.vo.wxcommentvo.UserInfo;
import com.cskaoyan.bean.vo.wxcommentvo.WxCommentCountVO;
import com.cskaoyan.bean.vo.wxcommentvo.WxCommentListVO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wei
 * @since 2022/09/09 10:59
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketUserMapper userMapper;

    @Override
    public CommonData queryCommentList(Integer valueId, byte type, int limit, int page, int showType,HttpSession session) {
        PageHelper.startPage(page, limit);
//        Integer userId=session.getAttribute("userId");

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);

        if (showType==1){
            criteria.andHasPictureEqualTo(true);
        }
        List<WxCommentListVO> commentListVOList = new ArrayList<>();

        List<MarketComment> marketComments = commentMapper.selectByExample(marketCommentExample);
        for (MarketComment marketComment : marketComments) {
            WxCommentListVO wxCommentListVO = new WxCommentListVO();
            BeanUtils.copyProperties(marketComment,wxCommentListVO);
            wxCommentListVO.setPicList(marketComment.getPicUrls());
            Integer userId = marketComment.getUserId();
            UserInfo userInfo = userMapper.selectNickNameAndAvatarUrlByUserId(userId);
            wxCommentListVO.setUserInfo(userInfo);
            commentListVOList.add(wxCommentListVO);
        }
//            WxCommentListVO wxCommentListVO = new WxCommentListVO();
//            BeanUtils.copyProperties(marketComment,wxCommentListVO);

        int num = marketComments.size();

        PageInfo<WxCommentListVO> pageInfo = new PageInfo<>(commentListVOList);
        int pages = pageInfo.getPages();
        CommonData<WxCommentListVO> CommonData = new CommonData<>();
        CommonData.setPage(page);
        CommonData.setTotal(num);
        CommonData.setLimit(limit);
        CommonData.setPages(pages);
        CommonData.setList(commentListVOList);
        return CommonData;
    }


    @Override
    public WxCommentCountVO countComment(Integer valueId, byte type) {
//  `value_id` 如果type=0，则是商品评论；如果是type=1，则是专题评论。',
//  `type` 评论类型，如果type=0，则是商品评论；如果是type=1，则是专题评论；',


        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andValueIdEqualTo(valueId);
        int allCount = (int) commentMapper.countByExample(marketCommentExample);


        MarketCommentExample marketCommentExample1 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria1 = marketCommentExample1.createCriteria();
        criteria1.andTypeEqualTo(type);
        criteria1.andValueIdEqualTo(valueId);
        criteria1.andHasPictureEqualTo(true);
        int hasPicCount=(int) commentMapper.countByExample(marketCommentExample1);
        WxCommentCountVO wxCommentCountVO = new WxCommentCountVO();
        wxCommentCountVO.setAllCount(allCount);
        wxCommentCountVO.setHasPicCount(hasPicCount);
        return wxCommentCountVO;
    }

    @Override
    public void postComment(WxCommentPostBO wxCommentPostBO, HttpSession session) {
//        int userId = session.getAttribute(userId);
        MarketUser user = (MarketUser) session.getAttribute("info");
        Integer userId=user.getId();
        MarketComment marketComment = new MarketComment();
        BeanUtils.copyProperties(wxCommentPostBO, marketComment);
        List<String> picUrls = (List<String>) wxCommentPostBO.getPicUrls();
        if (picUrls.size()>0){
            marketComment.setHasPicture(true);
        }
        marketComment.setUserId(userId);
        Date date = new Date(System.currentTimeMillis());
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketComment.setDeleted(false);
        commentMapper.insertSelective(marketComment);
    }
}

